package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.business.domain.Customer;
import cn.wolfcode.car.business.domain.Contract;
import cn.wolfcode.car.business.query.ContractQuery;
import cn.wolfcode.car.business.service.IContractService;
import cn.wolfcode.car.business.service.ICustomerService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import com.sun.mail.imap.protocol.ID;
import io.minio.errors.*;
import org.apache.poi.util.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

/**
 * 岗位控制器
 */
@Controller
@RequestMapping("business/contract")
public class ContractController {
    //模板前缀
    private static final String prefix = "business/contract/";

    @Autowired
    private IContractService contractService;

    @Autowired
    private ICustomerService iCustomerService;
    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:contract:view")
    @RequestMapping("/listPage")
    public String listPage(Model model) {
        List<Customer> customers = iCustomerService.selectAll();
        model.addAttribute("customers",customers);
        return prefix + "list";

    }

    @RequiresPermissions("business:contract:add")
    @RequestMapping("/addPage")
    public String addPage(Model model) {
        List<Customer> customerList = iCustomerService.selectAll();
        model.addAttribute("customerList",customerList);
        return prefix + "add";
    }


    @RequiresPermissions("business:contract:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model) {
        List<Customer> customerList = iCustomerService.selectAll();
        model.addAttribute("customerList",customerList);
        model.addAttribute("contract", contractService.get(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("business:contract:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<Contract> query(ContractQuery qo) {
        return contractService.query(qo);
    }


    //添加合同
    @RequiresPermissions("business:contract:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Contract contract) {
        contractService.save(contract);
        return AjaxResult.success();
    }

    //编辑
    @RequiresPermissions("business:contract:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(Contract contract) {
        contractService.update(contract);
        return AjaxResult.success();
    }

    //删除
    @RequiresPermissions("business:contract:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        contractService.deleteBatch(ids);
        return AjaxResult.success();
    }

    //文件附件上传
    @RequestMapping("/uploading")
    @ResponseBody
    public AjaxResult uploading(MultipartFile file) throws Exception {
        String path = contractService.uploading(file);
        return AjaxResult.success("上传成功",path);
    }
    //文件下载
    @RequestMapping("/downloadFile")
    @ResponseBody
    public AjaxResult downloadFile(Long id, HttpServletRequest request, HttpServletResponse response) throws IOException {
         String url = contractService.downloadFile(id);
         return AjaxResult.success("下载成功",url);

    }
    //审核通过
    @RequestMapping("/auditON")
    @ResponseBody
    public AjaxResult auditON(Long id){
        contractService.auditON(id);
        return AjaxResult.success();
    }
    //审核不通过
    @RequestMapping("/auditOff")
    @ResponseBody
    public AjaxResult auditOff(Long id){
        contractService.auditOff(id);
        return AjaxResult.success();
    }
    //盖章
    @RequestMapping("/affixSealOn")
    @ResponseBody
    public AjaxResult affixSealOn(Long id){
        contractService.affixSealOn(id);
        return AjaxResult.success();
    }
    //作废合同
    @RequestMapping("/isNullify")
    @ResponseBody
    public AjaxResult isNullify(Long id){
        contractService.isNullify(id);
        return AjaxResult.success();
    }
}
